package com.evolve.autotest.actions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;


public class ActionFactory {

	private static final Logger logger = Logger.getLogger(ActionFactory.class);
	
	private Map<String, IAction> actions;

	public ActionFactory() {
		actions = new HashMap<String, IAction>();
	}
	
	/**
	 * Gets an action from the action factory
	 * @param action
	 * @return
	 */
	public IAction getAction(final String action) {

		if (actions.containsKey(action)) {
			return actions.get(action);
		} else {
			logger.warn("The action " + action + " does not exist.");
			return null;
		}
	}
	
	/**
	 * Adds a collection of actions to the factory
	 * @param actionCollection
	 */
	public void fill(List<IAction> actionCollection) {
		
		if (actionCollection == null) {
			logger.info("No actions to load");
			return;
		}
		
		for (IAction action : actionCollection) {
			String name = action.getName();
			
			if (actions.containsKey(name)) {
				logger.warn("Action " + name + " already exist. Check for duplicates among actions.");
			} else {
				actions.put(name, action);
			}
		}
	}

	/**
	 * Checks if a action is available in the factory
	 * @param action
	 * @return
	 */
	public boolean containsAction(String action) {
		return actions.containsKey(action);
	}
}
